package com.ruoyi.easywenku.job;


import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.ruoyi.easywenku.entity.Question;
import com.ruoyi.easywenku.service.IQuestionService;
import com.ruoyi.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.util.ThreadContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component("answerJob")
public class AnswerJob {

    @Autowired
    IQuestionService questionService;

    public void execute() {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        ThreadContext.bind(manager);
        int answerMaxId = Integer.parseInt(StrUtil.emptyToDefault(RedisUtil.getStringValue("answerMaxId"), "100"));
        int errorCount = 0;
        while (true) {
            //http://tk.sqjob.cn/jxxw1382404.html
            HttpRequest request = HttpUtil.createGet("http://www.sqjob.cn/tiku/jxxw" + answerMaxId + ".html")
                    .header("Referer", "http://www.sqjob.cn/")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36");
            HttpResponse execute = request.execute();
            if (200 != execute.getStatus()) {
                answerMaxId++;
                errorCount++;
                if (errorCount > 1000) {
                    //RedisUtil.setStringValue("answerMaxId", String.valueOf(answerMaxId));
                    log.info("下载题目连续1000次失败.");
                    break;
                    //errorCount = 0;
                }
              //  if (answerMaxId > 1393947) {
              //      break;
              //  }
                //System.out.println(answerMaxId);
                continue;
            }

            try {
                String body = execute.body();
                //  System.out.println(body);
                int i = StrUtil.indexOfIgnoreCase(body, " padding-top:20px\">");
                int i2 = StrUtil.indexOfIgnoreCase(body, "</td>", i);
                if (i2 - i <= 0) {
                    answerMaxId++;
                    continue;
                }
                String title = StrUtil.sub(body, i + 20, i2).trim();
                i = StrUtil.indexOfIgnoreCase(body, "答案是:", i2);
                i2 = StrUtil.indexOfIgnoreCase(body, "<br>", i);
                String answer = StrUtil.sub(body, i + 4, i2).trim();
                i = StrUtil.indexOfIgnoreCase(body, "target=\"_blank\">", i2);
                i2 = StrUtil.indexOfIgnoreCase(body, "</a>", i);
                String source = StrUtil.sub(body, i + 16, i2).trim();

                Question question = new Question();

                question.setAnswer(answer);
                question.setQuestionBody(title);
                question.setRemarks1(source);
                question.setRemarks2(String.valueOf(answerMaxId));
                question.setCreateTime(new Date());
                questionService.save(question);
                RedisUtil.setStringValue("answerMaxId", String.valueOf(++answerMaxId));
            } catch (HttpException e) {
                Question question = new Question();
                question.setRemarks2(String.valueOf(answerMaxId++));
                questionService.save(question);
            }


        }
    }

}
